﻿using System;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
using QuickTest.Core.Compilation;
using QuickTest.Core.Events;
using QuickTest.Core.TestRunner;
using QuickTest.Core.Threading;
using QuickTest.UI.Controls;
using Rhino.Mocks;

namespace QuickTest.Tests.UI.Controls
{
    [TestFixture]
    public class TestRunnerControlTests
    {
        private TestRunnerControl testRunnerControl;

        [SetUp]
        public void SetUp()
        {
            testRunnerControl = new TestRunnerControl();
        }

        [Test]
        public void Can_Create_TestRunnerControl()
        {
            Assert.That(testRunnerControl, Is.InstanceOfType(typeof (TestRunnerControl)), "Should create instance of TestRunnerControl");
        }

        [Test]
        public void Can_Dispose_TestRunnerControl()
        {
            bool disposed = false;
            testRunnerControl.Disposed += (sender, e) => disposed = true;
            testRunnerControl.Dispose();

            Assert.That(disposed, "Should dispose control");
        }

        [Test]
        public void Can_Run_Tests()
        {
            var testLoader = MockRepository.GenerateStub<ITestLoader>();
            var testEvents = new EventHub();

            testRunnerControl.RunTests(testLoader, testEvents);

            testLoader.AssertWasCalled(l => l.Run<NUnitTestRunner>(testEvents));
        }

        [Test]
        public void Cannot_Run_Null_TestLoader()
        {
            Exception ex = TestUtils.AssertThrows<ArgumentNullException>(() => testRunnerControl.RunTests(null, new EventHub()), "Should throw ArgumentNullException if package is null");
            Assert.That((ex as ArgumentNullException).ParamName, Is.EqualTo("testLoader"));
        }
    }
}